<?php
/**
 * Created by PhpStorm.
 * User: chl
 * Date: 2017/3/21 0021
 * Time: 16:28
 */
namespace app\admin\controller;
use app\models\Bases;
use think\Request;

class Column extends Base{
    public function columnList(){
        $columnModel = new Bases('column');
        $result['list'] = $columnModel->selectData();
        return view('columnList',['result'=>$result]);
    }
    public function columnAdd(){
        $columnModel = new Bases('column');
        $advertModel = new Bases('advert');
        $adverColumn = new Bases('advert_column');
        try{
            if(Request()->isPost()){
                $post = input('param.');
                $validate = $this->validate($post,'Column.columnAdd');
                if($validate !== true){
                    throw new \LogicException($validate,1000);
                }
                $file = Request()->file('column_image');
                if(!empty($file)){
                    $info = $file
                        ->validate(['size'=>20000000000,'ext'=>'jpg,png,gif'])
                        ->rule('uniqid')
                        ->move(ROOT_PATH.'public'.DS.'uploads/column');
                    if(!$info){
                        throw new \LogicException($file->getError(),10056);
                    }
                }
                if(!empty($info)){
                    $post['column_image'] = $info->getFilename();
                }else{
                    unset($post['column_image']);
                }
                //多图广告
                if($post['type']==4){
                    if(empty($post['advert_ids'])){
                        throw new \LogicException('没有选择广告',4000);
                    }
                    $advert_ids = $post['advert_ids'];
                    unset($post['advert_ids']);
                }

                $rs = $columnModel->insertData($post);
                if($rs){
                    if($post['type']==4){
                        foreach($advert_ids as $v){
                            $adverColumn->insertData(['column_id'=>$rs,'advert_id'=>$v]);
                        }
                    }
                    return self::formatSuccessResult();
                }return self::formatResult(406,'操作失败');
            }
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
        $result = $advertModel->selectData('','advert_id,title');
        return view('columnAdd',['result'=>$result]);
    }
    public function columnSave(){
        $columnModel = new Bases('column');
        $adverColumn = new Bases('advert_column');
        $advertModel = new Bases('advert');
        try{
            $columnId = intval(input('column_id'));
            if(Request()->isPost()){
                $post = input('param.');
                $validate = $this->validate($post,'Column.columnSave');
                if($validate !== true){
                    throw new \LogicException($validate,1000);
                }
                $file = Request()->file('column_image');
                if(!empty($file)){
                    $info = $file
                        ->validate(['size'=>20000000000,'ext'=>'jpg,png,gif'])
                        ->rule('uniqid')
                        ->move(ROOT_PATH.'public'.DS.'uploads/column');
                    if(!$info){
                        throw new \LogicException($file->getError(),10056);
                    }
                }
                if(!empty($info)){
                    $post['column_image'] = $info->getFilename();
                }else{
                    unset($post['column_image']);
                }
                //多图广告
                if($post['type']==4){
                    if(empty($post['advert_ids'])){
                        throw new \LogicException('没有选择广告',4000);
                    }
                }

                //多图广告 如果有新的上传图片先获取再加上隐藏域的
                if($post['type']==4 && !empty($post['advert_ids'])){
                    $adverColumn->deleteByWhere('column_id ='.$post['column_id']);
                    foreach($post['advert_ids'] as $v){
                        $adverColumn->insertData(['column_id'=>$post['column_id'],'advert_id'=>$v]);
                    }
                }
                unset($post['advert_ids']);
                $rs = $columnModel->updateData($post);
                if($rs !== false){
                    return self::formatSuccessResult();
                }return self::formatResult(406,'操作失败');
            }
            $result['list'] = $advertModel->selectData('','advert_id,title');
            $adverColumnInfo = $adverColumn->selectData('column_id ='.$columnId,'advert_id');//根据栏目id查找出所有的多图广告并转换成字符串用in_arrray判断输出checked
            if(!empty($adverColumnInfo)){
                foreach( $adverColumnInfo as $k=>$v){
                    $advert_ids[$k] = $v['advert_id'];
                }
                $result['advert_ids'] = $advert_ids;
            }
            $columnOne= $columnModel->find('column_id ='.$columnId);
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
        return view('columnSave',['result'=>$columnOne,'advert_column'=>$result]);
    }
    public function columnDelete(){
        $columnId = intval(input('column_id'));
        $columnModel = new Bases('column');
        $advertColumnModel = new Bases('advert_column');
        $result = $advertColumnModel->find('column_id = '.$columnId);
        try{
            if(!empty($result)){
                throw new \LogicException('该栏目下有广告,请先删除所有广告',4000);
            }
            $rs = $columnModel->deleteByWhere('column_id = '.$columnId);
            if($rs ==false ){
                throw new \LogicException('操作失败',406);
            }
            return self::formatSuccessResult();

        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }


    }
}